//
//  FlatButton.swift
//  shaohuo
//
//  Created by User on 15/12/28.
//  Copyright © 2015年 lchenc3. All rights reserved.
//

import UIKit

class FlatButton: UIButton {

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        
        
        self.setupButtons()
    }
    
    required init(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)!
    }
    
    override func awakeFromNib() {
        self.setupButtons()
    }
    
    func setButtonSelectedState() -> Void {
     
        self.setBackgroundImage(UIImage(named: "button_normal_bg"), for: UIControlState())
        //self.setBackgroundImage(UIImage(named: "button_normal_bg"), forState: UIControlState.Normal)
        self.setBackgroundImage(UIImage(named: "button_normal_bg"), for: UIControlState.disabled)
        //self.setBackgroundImage(self.imageWithColorToButton(SHConstants.NoramlButtonBackgroundColor), forState: UIControlState.Highlighted)
        //self.setBackgroundImage(self.imageWithColorToButton(SHConstants.NoramlButtonBackgroundColor), forState: UIControlState.Disabled)
        
        self.setTitleColor(UIColor.black, for: UIControlState())
        self.setTitleColor(UIColor.black, for: UIControlState.disabled)
        self.setTitleColor(UIColor.lightGray, for: UIControlState.highlighted)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 15)
    }

    func setupButtons() -> Void {
        
        //self.layer.shouldRasterize = true
        
        self.setBackgroundImage(UIImage(named: "button_hlight_bg"), for: UIControlState())
        self.setBackgroundImage(UIImage(named: "button_normal_bg"), for: UIControlState.highlighted)
        self.setBackgroundImage(UIImage(named: "button_normal_bg"), for: UIControlState.disabled)
        //self.setBackgroundImage(self.imageWithColorToButton(SHConstants.NoramlButtonBackgroundColor), forState: UIControlState.Highlighted)
        //self.setBackgroundImage(self.imageWithColorToButton(SHConstants.NoramlButtonBackgroundColor), forState: UIControlState.Disabled)
        
        self.setTitleColor(UIColor.white, for: UIControlState())
        self.setTitleColor(UIColor.lightGray, for: UIControlState.highlighted)
        self.setTitleColor(UIColor.lightGray, for: UIControlState.disabled)
        self.titleLabel?.font = UIFont.systemFont(ofSize: 15)
    }
    
    func imageWithColorToButton(_ colorButton: UIColor) -> UIImage
    {
        let rect: CGRect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        let context: CGContext = UIGraphicsGetCurrentContext()!
        context.setFillColor(colorButton.cgColor)
        context.fill(rect)
        let imageReturn: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        
        return imageReturn
    }

}
